package it.avantsoft.ls.gl;

import it.avantsoft.ls.base.Protocol;
import it.avantsoft.ls.gl.command.*;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Collection;

public class GroupChildCommandFactory implements Protocol {
    public static GroupChildCommand getCommand(GroupChildHandler child, DataInputStream input, byte commandByte) {
        GroupChildCommand command = null;

        try {
            switch (commandByte) {
                case WELCOME://Nuovo Child in arrivo <- WelcomeMessage
                    int childListeningPort = input.readInt();
                    command = new WelcomeCommand(child, childListeningPort);
                    break;
                case ACK_NEW_CHILD:
                    String newChildId = input.readUTF();
                    String groupLeaderHost = input.readUTF();
                    int groupLeaderListeningPort = input.readInt();
                    command = new AckNewChildCommand(child, newChildId, groupLeaderHost, groupLeaderListeningPort);
                    break;
                case CONNECT:
                    String connectChildId = input.readUTF();
                    int connectChildListeningPort = input.readInt();
                    command = new ConnectCommand(child, connectChildId, connectChildListeningPort);
                    break;
                case PING:
                    command = new PingCommand(child);
                    break;
                case RECONNECT:
                    String reconnectChildId = input.readUTF();
                    String exGroupLeaderId = input.readUTF();
                    command = new ReconnectCommand(child, reconnectChildId, exGroupLeaderId);
                    break;
                case APPRAISAL:
                    int size = input.readInt();
                    byte[] b = new byte[size];
                    input.readFully(b);
                    command = new AppraisalCommand(child, b);
                    break;
                case NEWSON:
                    String id = input.readUTF();
                    Collection<GroupChildHandler> handlers = child.getLabHandlers(child.getChildId());
                    command = new NewGrandsonABCommand(handlers, id);
                    break;
                case DEADSON:
                    id = input.readUTF();
                    handlers = child.getLabHandlers(child.getChildId());
                    command = new DeadGrandsonABCommand(handlers, id);
                    break;
                case HORIZONTALEXCHANGEABLEPEER:
                    String exchangeablePeer = input.readUTF();
                    String designedGroupLeaderId = input.readUTF();
                    command = new ExchangeHorizontalPeerCommand(child, exchangeablePeer, designedGroupLeaderId);
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return command;
    }

}
